//-----------------------------------------------------------------------------
// UpdatePIN
//
// Copyright (c) MetaSwitch 2006
// Author: Alex Davies
//
// Sample application demonstrating the SOAP Sh interface to the MetaView
// Server.  This application sends in an Sh-Pull request to find a subscriber's
// current PIN, and then an Sh-Update request to change it to a new value.  It
// uses Apache Axis and the "typed" WSDL file, and also demonstrates bypassing
// the sequence number.
//
// See the readme file for more information.
//-----------------------------------------------------------------------------

import javax.xml.rpc.holders.*;

import com.MetaSwitch.EMS.SOAP.*;
import com.MetaSwitch.EMS.SOAP.holders.*;

public class UpdatePIN
{
  private final static String USAGE = "Usage: UpdatePIN " +
                                      "<Directory number> <New PIN>";
  private static ShTypedUtilities utilities = new ShTypedUtilities();

  public static void main(String[] args)
  {
    StringHolder dNHolder = new StringHolder();
    StringHolder pinHolder = new StringHolder();

    IntHolder resultCodeHolder = new IntHolder();
    TExtendedResultHolder extendedResultHolder = new TExtendedResultHolder();
    TUserDataHolder userDataHolder = new TUserDataHolder();

    try
    {
      //-----------------------------------------------------------------------
      // Extract information from the command line arguments.
      //-----------------------------------------------------------------------
      getParameters(args, dNHolder, pinHolder);

      //-----------------------------------------------------------------------
      // Connect to the Sh service and send in a Pull request.
      //-----------------------------------------------------------------------
      ShServiceLocator locator = new ShServiceLocator();

      Sh shInstance = locator.getShService();

      shInstance.shPull(dNHolder.value,
                        0,
                        "Meta_Subscriber_BaseInformation",
                        utilities.ORIGIN_HOST,
                        resultCodeHolder,
                        extendedResultHolder,
                        userDataHolder);

      //-----------------------------------------------------------------------
      // Check that the request succeeded and extract and display the current
      // PIN.
      //-----------------------------------------------------------------------
      utilities.checkResultCode(resultCodeHolder.value,
                                extendedResultHolder.value,
                                userDataHolder.value);

      TMetaSwitchData metaSwitchData = userDataHolder.value
                                                     .getShData()
                                                     .getRepositoryData()
                                                     .getServiceData()
                                                     .getMetaSwitchData();

      TMeta_Subscriber_BaseInformation baseInformation =
                           metaSwitchData.getMeta_Subscriber_BaseInformation();

      System.out.println("Old PIN: " + baseInformation.getPIN());

      //-----------------------------------------------------------------------
      // Update the user data with the new PIN, then send in the Update request
      // and check it succeeded.
      //-----------------------------------------------------------------------
      baseInformation.setPIN(pinHolder.value);

      shInstance.shUpdate(
                      dNHolder.value,
                      0,
                      userDataHolder.value,
                      utilities.ORIGIN_HOST + utilities.IGNORE_SEQUENCE_NUMBER,
                      resultCodeHolder,
                      extendedResultHolder);

      utilities.checkResultCode(resultCodeHolder.value,
                                extendedResultHolder.value,
                                userDataHolder.value);
      System.out.println("PIN successfully changed to " + pinHolder.value);
    }
    catch (MetaSwitchShInterfaceException e)
    {
      System.err.println(e.getMessage());
      System.err.println(USAGE);
    }
    catch (org.apache.axis.AxisFault e)
    {
      System.err.println("Problem connecting to MetaView server: " + e);
      System.err.println(USAGE);
    }
    catch (javax.xml.rpc.ServiceException e)
    {
      System.err.println("Could not connect to Sh service");
      System.err.println(USAGE);
    }
    catch (Exception e)
    {
      System.err.println("Unexpected error \"" + e
                         + "\" in retrieving data");
      e.printStackTrace(System.err);
      System.err.println(USAGE);
    }
  }

  /**
   * Parse the command line arguments and extract the necessary information.
   *
   * Both parameters are required:
   * -  the subscriber's DN
   * -  the new PIN to set.
   *
   * @param args        IN  The arguments provided at the command line.
   * @param dN          OUT The directory number to update.
   * @param pin         OUT The new PIN to use for this subscriber.
   *
   * @exception WrongParametersException
   */
  private static void getParameters(String[] args,
                                    StringHolder dN,
                                    StringHolder pin)
    throws WrongParametersException
  {
    if (args.length != 2)
    {
      throw new WrongParametersException("Wrong number of parameters.");
    }

    dN.value = args[0];
    pin.value = args[1];
  }
}
